/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "ch32v30x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"
#include <board.h>
#include "easyblink.h"

/* Global typedef */

/* Global define */
#define AHT10_I2C_BUS_NAME          "i2c1"  /* 传感器连接的I2C总线设备名称 */

#define AHT10_ADDR                  0x38    /* 从机地址 */
#define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
#define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */
#define AHT10_GET_DATA              0xAC    /* 获取数据命令 */

#define AHT10_ADDR                  0x38    /* 从机地址 */
#define AHT10_CALIBRATION_CMD       0xE1    /* 校准命令 */
#define AHT10_NORMAL_CMD            0xA8    /* 一般命令 */


#define DATDACOMMIT_UART__BUS_NAME          "uart7"  /* 上传数据设备名称 */

#define MPU6050_I2C_BUS_NAME          "i2c2"  /* 传感器连接的I2C总线设备名称 */
#define MPU6050_ADD     0x69


/* Global Variable */







struct rt_semaphore *TimeoutGlobale;
struct mpu6xxx_device *MPU6050Globale;



void SensorPeriodRead(void *args);
void datatrans(void *args);

int main(void)
{

    rt_err_t error = RT_NULL;
    rt_timer_t  datatranstimer = RT_NULL;
    struct rt_semaphore *timeout;





    timeout = rt_sem_create ("timeout", 0, RT_IPC_FLAG_FIFO);

    if(timeout == RT_NULL)
        rt_kprintf("datatrans_timer is wrong");


    datatranstimer =  rt_timer_create("100ms_timer",
                                    rt_sem_release,
                                    timeout,
                                    1000,
                                    RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_CTRL_SET_PERIODIC);

    if(datatranstimer != RT_NULL){
        rt_timer_start  (datatranstimer);
    }
    else {
        rt_kprintf("datatrans_timer is wrong");
    }




    rt_enter_critical();
    TimeoutGlobale = timeout;
    rt_exit_critical();
}






void user_device_init()
{
    rt_err_t error = ERROR;
    ebled_t LED1 = {0};
    ebled_t LED2 = {0};
    rt_device_t user_device_init;

    struct mpu6xxx_device *MPU6050;
    struct rt_device_pwm *pwm_dev;

    LED1 = easyblink_init_led(0,  PIN_LOW);
    LED2 = easyblink_init_led(1,  PIN_LOW);

    easyblink( LED1, -1, 400, 500);
    easyblink( LED2, -1, 250, 500);


    user_device_init =  rt_device_find(DATDACOMMIT_UART__BUS_NAME);

    if(user_device_init != RT_NULL)
    {
        error = rt_device_open(user_device_init, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
                RT_DEVICE_FLAG_STREAM);

        if(error != RT_EOK)

            rt_kprintf("errorr: %d\n",error);
    }

    user_device_init = RT_NULL;
    error = RT_ERROR;



    user_device_init = rt_device_find(MPU6050_I2C_BUS_NAME);

    if(user_device_init != RT_NULL)
    {
        error = rt_device_open(user_device_init, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | \
                        RT_DEVICE_FLAG_STREAM);

        if(error == RT_EOK)
            MPU6050 = mpu6xxx_init(MPU6050_I2C_BUS_NAME, RT_NULL);
        else
            rt_kprintf("errorr: %d\n",error);

    }

    //rt_device_pwm_register(user_pwm_device,"servo", const struct rt_pwm_ops *ops, const void *user_data);
    rt_enter_critical();

    MPU6050Globale = MPU6050;

    rt_exit_critical();

    rt_kprintf("MCU: CH32V307\n");
    rt_kprintf("SysClk: %d MHz\n",SystemCoreClock/1000000);
    rt_kprintf("www.wch.cn\n");
}


INIT_DEVICE_EXPORT(user_device_init);


void LED_BLINK_INIT(void)
{

    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

INIT_BOARD_EXPORT(LED_BLINK_INIT);

